﻿using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using XLua;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx
{
    public abstract class ScreenOrientationChangeHandler : MonoBehaviour, IScreenOrientDetectorListener
    {
        public virtual int Order => (int)AdapterExecuteOrder.Default;
        public void Awake()
        {
            //Debug.Log("Awake..............");
            //ScreenOrientDetector.GetInstance().AddOrientationChangeListener(this);
            
        }
        public void OnDestroy()
        {
            //Debug.Log("OnDestroy..............");
            //ScreenOrientDetector.GetInstance().RemoveOrientationChangeListener(this);
        }

        protected virtual void LateUpdate() {
        
        }

        public bool IsyPortrait()
        {
    
#if UNITY_EDITOR
            var size = GameViewSize();
            return size.x < size.y;
#else
                 var currentOrientation = ScreenOrientManager.GetInstance().CurrentLogicOrientation;
            return currentOrientation == UnityEngine.ScreenOrientation.PortraitUpsideDown ||
                   currentOrientation == UnityEngine.ScreenOrientation.Portrait; 
#endif
        }
        public bool IsLandscape()
        {
#if UNITY_EDITOR
            var size = GameViewSize();
            return size.x > size.y;
#else
            var currentOrientation = ScreenOrientManager.GetInstance().CurrentLogicOrientation;
            return currentOrientation == UnityEngine.ScreenOrientation.LandscapeLeft ||
                   currentOrientation == UnityEngine.ScreenOrientation.LandscapeRight; 
#endif
          
        }
        
        public abstract void ScreenOrientationChange(UnityEngine.ScreenOrientation orientation,bool needForceApply = false);

        public virtual void ApplyLandscapeConfig() { }

        public virtual void ApplyPortraitConfig() { }

        public virtual void SetInvalid(){ }
        /// <summary>
        /// 保存当前模式下数据
        /// </summary>
        public virtual bool SaveConfigData()
        {
            return true;
        }

        /// <summary>
        /// 横竖数据一样就可以清理
        /// </summary>
        public virtual bool IsCanCleanup()
        {
            return false;
        }
        
#if UNITY_EDITOR
        public void OnValidate()
        {
        }
        
        
          private Vector2Int GameViewSize()
        {
            
            var mouseOverWindow = UnityEditor.EditorWindow.mouseOverWindow;
            System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
            System.Type type = assembly.GetType("UnityEditor.PlayModeView");

            Vector2 size = (Vector2)type.GetMethod(
                "GetMainPlayModeViewTargetSize",
                System.Reflection.BindingFlags.NonPublic |
                System.Reflection.BindingFlags.Static
            ).Invoke(mouseOverWindow, null);

            return new Vector2Int((int)size.x,(int)size.y);
        }
        [BlackList]
        public virtual bool HasComponent()
        {
            return true;
        }
#endif
       
    }
}